package example.untils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import example.dto.Children;
import example.dto.ChildrenInfo;
import example.dto.ChildrenInfoList;
import example.dto.Product;
import example.services.MyTestServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class JsonParse {
    private static JSONArray jsonArray;
    private static JSONObject jsonObject;
    @Autowired
    private MyTestServices myTestServices;


    //获取主商品
    public  List<Product> getProducts(){
        List<Product> productList=new ArrayList<>();
        for(int i=0;i<jsonArray.size();i++){
            Product product=new Product();
            product.setPname(jsonArray.getJSONObject(i).getString("optName"));
            product.setPOptId(jsonArray.getJSONObject(i).getString("optId"));
            productList.add(product);
        }
        return productList.size()>0?productList:null;
    }


    /**
     *  插入主商品下的子商品
     * @param productName 主商品名称
     * @return 子商品的集合
     */
    public  List<Children> getChildrens(String productName){
        List<Children> childrenList=new ArrayList<>();
        System.out.println("==============================="+productName);
        for(int i=0;i<jsonArray.size();i++){
            if(jsonArray.getJSONObject(i).getString("optName").equals(productName)){
                JSONArray jsonArray1=jsonArray.getJSONObject(i).getJSONArray("list");
                for(int j=0;j<jsonArray1.size();j++){
                    JSONArray jsonArray2=jsonArray1.getJSONObject(j).getJSONArray("list");
                    for(int k=0;k<jsonArray2.size();k++){
                        Children children=new Children();
                        String imageUrl=jsonArray2.getJSONObject(k).getString("imageUrl");
                        String linkUrl=jsonArray2.getJSONObject(k).getString("linkUrl");
                        String optId=jsonArray2.getJSONObject(k).getString("optId");
                        String optName=jsonArray2.getJSONObject(k).getString("optName");
                        String idx=jsonArray2.getJSONObject(k).getString("idx");

                        children.setCImageUrl(imageUrl==null?"":imageUrl);
                        children.setCLinkUrl(linkUrl==null?"":linkUrl);
                        children.setCOptId(optId==null?"":optId);
                        children.setCName(optName==null?"":optName);
                        children.setCIdx(idx==null?"":idx);
                        String pid=myTestServices.findProduct_ID(productName).toString();
                        children.setPid(pid);
                        childrenList.add(children);
                    }
                }
            }
        }
        return childrenList.size()>0?childrenList:null;
    }

    //获取子商品列表
    public List<ChildrenInfoList> getChildrenInfoList(String cid,String cName,String optName){
        List<ChildrenInfoList> childrenInfoLists=new ArrayList<>();
        for(int i=0;i<jsonArray.size();i++){
            ChildrenInfoList childrenInfoList=new ChildrenInfoList();
            if(optName.equals(cName)){
                String imageUrl=jsonArray.getJSONObject(i).getString("imgUrl");
                String goodsName=jsonArray.getJSONObject(i).getString("goodsName");
                String price=jsonArray.getJSONObject(i).getString("price");
                String salesTip=jsonArray.getJSONObject(i).getString("salesTip");
                String linkURL=jsonArray.getJSONObject(i).getString("linkURL");

                childrenInfoList.setCi_imaUrl(imageUrl==null?"":imageUrl);
                childrenInfoList.setCi_goodsName(goodsName==null?"":goodsName);
                childrenInfoList.setCi_price(price==null?"":price);
                childrenInfoList.setCi_salesTip(salesTip==null?"":salesTip);
                childrenInfoList.setCi_linkURL(linkURL==null?"":linkURL);
                childrenInfoList.setCid(cid);
                JSONArray jsonArray1=jsonArray.getJSONObject(i).getJSONArray("tagList");
                for(int j=0;j<jsonArray1.size();j++){
                    String text=jsonArray1.getJSONObject(j).getString("text");
                    childrenInfoList.setCi_text(text);
                    childrenInfoLists.add(childrenInfoList);
                }
            }
        }
        return childrenInfoLists.size()>0?childrenInfoLists:null;
    }



    //获取商品详情
    public List<ChildrenInfo> getChildrenInfos(){
        List<ChildrenInfo> childrenInfoList=new ArrayList<>();
        ChildrenInfo childrenInfo=new ChildrenInfo();
        String goodsName=jsonObject.getString("goodsName");//商品描述
        String goodsID=jsonObject.getString("goodsID");//商品ID
        JSONArray skus=jsonObject.getJSONArray("skus");//商品样式
        String linePrice=jsonObject.getString("linePrice");//原价
        String sideSalesTip=jsonObject.getString("sideSalesTip"); //总售出量
        String viewImageData= jsonObject.getJSONArray("viewImageData").toString();//商品图片URL

//        String groupPrice=jsonArray1.getJSONObject(0).getString("groupPrice"); //商品补贴价格
//        String normalPrice=jsonArray1.getJSONObject(0).getString("normalPrice");//商品单独购买价格
//        String groupTip=jsonArray1.getJSONObject(0).getString("groupTip");//券后价
        String goodsProperty=jsonObject.getJSONArray("goodsProperty").toString();//商品参数

        childrenInfo.setCh_goodsName(goodsName==null?"":goodsName);
        childrenInfo.setCh_sideSalesTip(sideSalesTip==null?"":sideSalesTip);
        childrenInfo.setCh_viewImageUrl(viewImageData==null?"":viewImageData);
//        childrenInfo.setCh_groupPrice(groupPrice==null?"":groupPrice);
//        childrenInfo.setCh_normalPrice(normalPrice==null?"":normalPrice);
        childrenInfo.setCh_linePrice(linePrice==null?"":linePrice);
//        childrenInfo.setCh_groupTip(groupTip==null?"":groupTip);
        childrenInfo.setCh_canshu(goodsProperty==null?"":goodsProperty);
        childrenInfo.setCh_goodsID(goodsID==null?"":goodsID);
        childrenInfo.setSkus(skus.toJSONString()==null?"":skus.toJSONString());
        childrenInfoList.add(childrenInfo);
        return childrenInfoList.size()>0?childrenInfoList:null;
    }



    //解析json主体
    public static void parseJson(String json,String rootItems){
        JSONObject jsonObject= JSON.parseObject(json);
        jsonArray=jsonObject.getJSONObject("store").getJSONObject("data").getJSONArray(rootItems);
    }

    //解析子商品列表json主体
    public static void parseJson_children(String json,String ssrListData){
        JSONObject jsonObject= JSON.parseObject(json);
        jsonArray=jsonObject.getJSONObject("store").getJSONObject("data").getJSONObject(ssrListData).getJSONArray("list");
    }
    //解析子商品列表json主体,得到optName
    public  String parseJson_children_optName(String json){
        JSONObject jsonObject= JSON.parseObject(json);
        return jsonObject.getJSONObject("store").getJSONObject("data").getJSONObject("urlInfo").getString("optName");
    }

    //解析商品详情json主体,得到optName
    public static void parseJson_children_goods(String json){
        JSONObject jsonObject1= JSON.parseObject(json);
        jsonObject= jsonObject1.getJSONObject("store").getJSONObject("initDataObj").getJSONObject("goods");
    }

}
